class Solution(object):
    def smallestSubsequence(self, s, k, letter, repetition):
        n = len(s)
        suffix_l = [0] * n
        suffix_s = [n] * n
        for i in range(n - 1, -1, -1):
            if i == n - 1:
                if s[i] == letter:
                    suffix_l[i] = 1
            else:
                suffix_l[i] = suffix_l[i + 1] + (s[i] == letter)
        st = []
        for i in range(n):
            while st and s[i] < s[st[-1]]:
                last_index = st.pop()
                suffix_s[last_index] = i
            st.append(i)
        ans = []
        left_k = k
        left_r = repetition
        i = 0
        while i < n and left_k > 0:
            if left_k == left_r:
                ans += [letter] * left_k
                break
            next_i = suffix_s[i]
            if next_i < n:
                next_r = suffix_l[next_i]
                if n - next_i < left_k or next_r < left_r:
                    ans.append(s[i])
                    left_k -= 1
                    if s[i] == letter:
                        left_r -= 1
            else:
                ans.append(s[i])
                if s[i] == letter:
                    left_r -= 1
                    left_k -= 1
                else:
                    left_k -= 1
            i += 1
        return ''.join(ans)



                    

data = Solution()
s = "leet"
k = 3
letter = "e"
repetition = 1
print(data.smallestSubsequence(s, k, letter, repetition))